import { Component, OnInit } from '@angular/core';

import {StorageService} from "../../services/storage.service";

@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.less']
})
export class SearchComponent implements OnInit {
  public keyWord:any;
  public searchHistory:any[] = [];

  constructor(public storage:StorageService) { }

  ngOnInit() {
    console.log('页面初始化');
    let searchList:any = this.storage.get('searchList');
    if(searchList){
      this.searchHistory = searchList;
    }
  }
  doSearch(e){
    if(e.keyCode === 13 || e === 13){
      if(this.keyWord){
        if(this.searchHistory.indexOf(this.keyWord) === -1){
          this.searchHistory.push(this.keyWord);
          this.storage.set('searchList',this.searchHistory);
        }
      }
      this.keyWord = '';
    }

  }
  deleteHistory(index){
    this.searchHistory.splice(index,1);
    this.storage.set('searchList',this.searchHistory);
  }
}
